<?php


namespace App\Http\Controllers\Admin\User;


use App\Http\Controllers\Admin\BaseController;
use App\Http\Requests\Admin\User\Menu\CreateRequest;
use App\Http\Requests\Admin\User\Menu\UpdateRequest;
use App\Http\Requests\Admin\User\MenuGroup\CreateRequest as CreateMenuGroupRequest;
use App\Http\Requests\Admin\User\MenuGroup\UpdateRequest as UpdateMenuGroupRequest;
use App\Models\Menu;
use App\Models\MenuGroup;
use App\Repositories\Admin\User\MenuRepository;
use Illuminate\Http\Request;

class MenuController extends BaseController
{
    public function __construct(){
        $this->repo = new MenuRepository();
    }

    public function fetchInfo(Menu $menu){
        $menu->load(['permissions','roles']);
        return $this->success($menu);
    }
    public function fetchList(Request $request){
        $gid = $request->get('gid');
        $tree = $this->repo->menuTree($gid);
        return $this->success(compact('tree'));
    }
    public function menuSelect(Request $request){
        $type = $request->get('type',Menu::TYPE_SECOND_MENU);
        $gid = $request->get('gid');
        if(!$gid||!in_array($type,[Menu::TYPE_SECOND_MENU,Menu::TYPE_THIRD_MENU])){
            return $this->error("参数错误");
        }
        $data = $this->repo->menuSelect($type,$gid);
        return $data?$this->success($data):$this->error();
    }
    public function create(CreateRequest $request){
        $data = $request->only('menu_group_id','type','sort','status','pid','label','route','icon','permissions','roles','is_auth');
        $menu = $this->repo->create($data);
        return $menu?$this->success($menu,'添加成功'):$this->error('添加失败');
    }
    public function update(UpdateRequest $request,Menu $menu){
        $data = $request->only('sort','status','label','route','icon','permissions','roles','is_auth');
        $menu = $this->repo->update($menu,$data);
        return $menu?$this->success($menu,'更新成功'):$this->error('更新失败');
    }
    public function delete(Menu $menu){
        if($menu->children&&$menu->children->count()){
            return $this->error('该菜单下还有子菜单');
        }
        return $menu->delete()?$this->success([],"删除成功"):$this->error("删除失败");
    }
    public function menuSetStatus(Menu $menu){
        return $this->repo->update($menu, ['status' => !$menu->status]) ?
            $this->success([], '更新成功') : $this->error('更新失败');
    }
    /*
     * 菜单组
     */
    /**
     * 菜单组列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function menuGroup(Request $request){
        $status = $request->get('status',-1);
        $size = $request->get('size',config('admin.per_page'));
        $data = $this->repo->menuGroups($status,$size);
        return $this->success($data);
    }

    /**
     * 查询单挑菜单组
     * @param MenuGroup $menuGroup
     * @return \Illuminate\Http\JsonResponse
     */
    public function fetchMenuGroup(MenuGroup $menuGroup){
        return $this->success($menuGroup);
    }

    /**
     * 新建菜单组
     * @param CreateMenuGroupRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function createMenuGroup(CreateMenuGroupRequest $request){
        $menuGroup = $this->repo->createMenuGroup($request->only('title','status'));
        return $menuGroup?$this->success($menuGroup,'添加成功'):$this->error();
    }

    /**
     * 更新菜单组
     * @param UpdateMenuGroupRequest $request
     * @param MenuGroup $menuGroup
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateMenuGroup(UpdateMenuGroupRequest $request,MenuGroup $menuGroup){
        $menuGroup = $this->repo->updateMenuGroup($menuGroup,$request->only('title','status'));
        return $menuGroup?$this->success($menuGroup,'更新成功'):$this->error();
    }
    /**
     * 修改菜单组状态
     * @param MenuGroup $menuGroup
     * @return \Illuminate\Http\JsonResponse
     */
    public function menuGroupSetStatus(MenuGroup $menuGroup)
    {
        return $this->repo->update($menuGroup, ['status' => !$menuGroup->status]) ?
            $this->success([], '更新成功') : $this->error('更新失败');
    }

    /**
     * 删除菜单组
     * @param MenuGroup $menuGroup
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function menuGroupDelete(MenuGroup $menuGroup){
        $deleted = $this->repo->menuGroupDelete($menuGroup);
        return $deleted?$this->success([], '删除成功') : $this->error('删除失败');
    }
}
